package com.example.demo;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class ResponseController {
    // 方式1， 使用HttpServletResponse
    @RequestMapping("/response")
    public void response(HttpServletResponse  response) throws IOException {
        // 设置状态码
        response.setStatus(401);
        // 设置响应头
        response.setHeader("X-Powered-By", "SpringBoot");
        // 设置响应体
        response.getWriter().write("<h1>Hello Response</h1>");
    }

    // 方式2，使用ResponseEntity
    @RequestMapping("/response2")
    public ResponseEntity<String> response2() {
        return ResponseEntity.status(200).header("X-Powered-By", "SpringBoot").body("<h1>Hello Response</h1>");
    };
}
